package com.weilele.leisure.time.ui.module.main

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView


private data class AdapterBean(
        val viewType: Int,
        val holder: Class<*>,
        val bean: Class<*>,
        val spanSize: Int
)

private const val TYPE_SIMPLE_TEXT = 1
private const val TYPE_LOADING = 2
private const val TYPE_BANNER = 3
private const val TYPE_VIDEO_ITEM = 4
private const val TYPE_VIDEO_ITEM_PLACEHOLDER = 5

//item类型 搜索
const val ITEM_TYPE_SEARCH = "search"

//item类型 轮播图
const val ITEM_TYPE_BANNER = "banner"

//item类型 tab
const val ITEM_TYPE_TAB_ITEM = "tab_item"

//item类型 有限的视频集合
const val ITEM_TYPE_VIDEO_LIST = "video_list"

//item类型 可加载更多的视频集合
const val ITEM_TYPE_VIDEO_DRAG_LIST = "video_drag_list"

//item类型 广告
const val ITEM_TYPE_AD = "ad"
/*
private val adapterBeans by lazy {
    listOf(
            AdapterBean(
                    TYPE_SIMPLE_TEXT,
                    SimpleTextTitleHolder::class.java,
                    SimpleTextTitleHolder.Bean::class.java,
                    3
            ),
            AdapterBean(
                    TYPE_LOADING,
                    LoadingHolder::class.java,
                    LoadingHolder.Bean::class.java,
                    3
            ),
            AdapterBean(
                    TYPE_BANNER,
                    HomeBannerHolder::class.java,
                    HomeBannerHolder.Bean::class.java,
                    3
            ),
            AdapterBean(
                    TYPE_VIDEO_ITEM,
                    VideoItemHolder::class.java,
                    VideoItemHolder.Bean::class.java,
                    1
            ),
            AdapterBean(
                    TYPE_VIDEO_ITEM_PLACEHOLDER,
                    PlaceholderHolder::class.java,
                    PlaceholderHolder.Bean::class.java,
                    3
            ),
    )
}

private fun arePageContentSame(old: Any?, new: Any?): Boolean {
    return when {
        old is SimpleTextTitleHolder.Bean
                && new is SimpleTextTitleHolder.Bean -> {
            old.title == new.title
                    && old.hint == new.hint
                    && old.hintCoin == new.hintCoin
        }
        old is LoadingHolder.Bean
                && new is LoadingHolder.Bean -> {
            //必须返回false，否在无法触发加载回调
            false
        }
        old is HomeBannerHolder.Bean
                && new is HomeBannerHolder.Bean -> {
            old.data.id == new.data.id
        }
        old is VideoItemHolder.Bean
                && new is VideoItemHolder.Bean -> {
            old.data.id == new.data.id
        }
        old is PlaceholderHolder.Bean
                && new is PlaceholderHolder.Bean -> {
            true
        }
        else -> false
    }
}

private fun arePageItemSame(old: Any?, new: Any?): Boolean {
    return old == new
}

fun MvvmRcvAdapter<Any, *>.refreshHomePage(bean: MutableList<Any>?) {
    this.refresh(bean, ::arePageItemSame, ::arePageContentSame)
}


private val adapterHashMap by lazy {
    val map = hashMapOf<Int, Class<*>>()
    adapterBeans.forEach {
        map[it.viewType] = it.holder
    }
    map
}

fun createRecommendAdapter() = mvvmRcvAdapter<Any>(adapterHashMap, null, null).apply {
    setOnGetItemViewTypeListener { self, position ->
        getAdapterBean(datas, position).viewType
    }
}


private fun getAdapterBean(datas: MutableList<Any>, position: Int): AdapterBean {
    val data = datas[position]
    adapterBeans.forEach {
        if (data::class.java == it.bean) {
            return it
        }
    }
    throw IllegalArgumentException("viewType和数据类型不匹配,数据类型是:${datas[position]}")
}

fun PageByIdBean?.toPageDatas(hasMore: Boolean = false): MutableList<Any>? {
    val datas = this?.components ?: return null
    val newList = mutableListOf<Any>()
    datas.forEach {
        when (it.type) {
            ITEM_TYPE_BANNER -> {
                newList.add(HomeBannerHolder.Bean(it))
            }
            ITEM_TYPE_SEARCH -> {

            }
            ITEM_TYPE_TAB_ITEM -> {
            }
            ITEM_TYPE_VIDEO_LIST, ITEM_TYPE_VIDEO_DRAG_LIST -> {
                newList.add(SimpleTextTitleHolder.Bean(it.title, "", null))
                val videos = it.videos
                if (videos.isNullOrEmpty()) {
                    newList.add(PlaceholderHolder.Bean())
                } else {
                    newList.addAll(videos.toVideoItemBeans())
                }
            }
            ITEM_TYPE_AD -> {
            }
        }
    }
    if (hasMore) {
        newList.add(LoadingHolder.Bean("别催啦，正在拼命~", true))
    } else {
        newList.add(LoadingHolder.Bean("世界虽不完美，但我们仍可治愈自己", false))
    }
    return newList
}

fun MutableList<VideoListBean.RowsDTO>.toVideoItemBeans(): MutableList<Any> {
    return this.map {
        val video = it
        if (video.pic != null) {
            if (video.pic.startsWith("mac://")) {
                video.pic = video.pic?.replace("mac://", "https://")
            }
        }
        VideoItemHolder.Bean(video)
    }.toMutableList()
}

fun RecyclerView.setPageLayoutManage(adapter: MvvmRcvAdapter<Any, *>) {
    val manager = GridLayoutManager(this.context, 3)
    manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
        override fun getSpanSize(position: Int): Int {
            return getAdapterBean(adapter.datas, position).spanSize
        }
    }
    this.layoutManager = manager
}*/
